import {create} from 'zustand';
import createSelectors from "@/lib/createSelectors";
import {createJSONStorage} from "zustand/middleware";
import storage from "@/utils/storage";
import {createUserSlice} from "@/store/modules/user";
import {immer} from "zustand/middleware/immer";
import {devtools, persist} from "zustand/middleware";
import {UserSliceState, AppSliceState, SpinnerState} from "@/store/types";
import {createAppSlice} from "@/store/modules/app";
import {createSpinnerSlice} from "@/store/modules/spinner";

const id = "appStore";

type StoreState = UserSliceState & AppSliceState & SpinnerState;

const blockList = ["showSpinner", "loadingDuration", "loadingText"]
export const useStore = createSelectors(create<StoreState>()(
        immer(devtools(persist((...args) => ({
            ...createUserSlice(...args),
            ...createAppSlice(...args),
            ...createSpinnerSlice(...args)
        }), {
            name: id,
            storage: createJSONStorage(() => storage),
            skipHydration: false,//是否跳过在初始化时获取缓存
            partialize: (state) =>
                Object.fromEntries(
                    Object.entries(state).filter(([key]) => !blockList.includes(key)),
                ),
        })))
        // middlewares<UserSliceState>((...args) => ({
        //     ...createUserSlice(...args),
        // }), {
        //     name: id,
        //     storage: createJSONStorage(() => storage),
        //     skipHydration: false,//是否跳过在初始化时获取缓存
        // })
    ))
;

